<template>
  <BasicModal v-bind="$attrs" @register="registerModal" :title="getTitle" @ok="handleSubmit">
    <BasicForm @register="registerForm" />
  </BasicModal>
</template>
<script lang="ts">
  import { defineComponent, ref, computed, unref } from 'vue';
  import { BasicModal, useModalInner } from '/@/components/Modal';
  import { BasicForm, useForm } from '/@/components/Form/index';
  import { accountFormSchema } from './account.data';
  import { getDeptList } from '/@/api/sys/dept';
  import { useMessage } from '/@/hooks/web/useMessage';
  import { createAccount, updateAccount, searchAccount } from '/@/api/sys/account';
  export default defineComponent({
    name: 'AccountModal',
    components: { BasicModal, BasicForm },
    emits: ['success', 'register'],
    setup(_, { emit }) {
      //加载页面时的初始化
      const isUpdate = ref(true);
      const rowId = ref('');
      const { createMessage } = useMessage();
      const [registerForm, { setFieldsValue, updateSchema, resetFields, validate }] = useForm({
        labelWidth: 100,
        schemas: accountFormSchema,
        showActionButtonGroup: false,
        actionColOptions: {
          span: 23,
        },
      });

      const [registerModal, { setModalProps, closeModal }] = useModalInner(async (data) => {
        resetFields();
        setModalProps({ confirmLoading: false });
        isUpdate.value = !!data?.isUpdate;

        if (unref(isUpdate)) {
          rowId.value = data.record.id;
          //查询当前最新信息
          const records = await searchAccount(rowId.value);
          setFieldsValue({
            ...records,
          });
        }

        const treeData = await getDeptList();

        //在这里更新组件的状态
        updateSchema([
          {
            field: 'password',
            ifShow: !unref(isUpdate),
          },
          {
            field: 'userName',
            componentProps: { readonly: unref(isUpdate) },
          },
          {
            field: 'departmentId',
            componentProps: { treeData },
          },
        ]);
      });

      const getTitle = computed(() => (!unref(isUpdate) ? '新增账号' : '编辑账号'));

      async function handleSubmit() {
        try {
          const values = await validate();
          setModalProps({ confirmLoading: true });

          values.id = unref(rowId);

          var success;
          if (unref(isUpdate)) {
            success = updateAccount(values);
            //成功之后才能关闭界面
            success.then(() => {
              createMessage.success('操作成功');
              closeModal();
              emit('success');
            });
          } else {
            success = createAccount(values);
            //成功之后才能关闭界面
            success.then(() => {
              createMessage.success('操作成功');
              closeModal();
              emit('success');
            });
          }
        } finally {
          setModalProps({ confirmLoading: false });
        }
      }

      return { registerModal, registerForm, getTitle, handleSubmit };
    },
  });
</script>
